package temaihui.daimaitemai;

import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.nostra13.universalimageloader.core.ImageLoader;

import org.json.JSONObject;

import java.util.List;

import temaihui.daimaitemai.adapter.CategoryGridAdapter;
import temaihui.daimaitemai.adapter.ItemDetailGridAdapter;

public class CategoryActivity extends FragmentActivity {

    int source;//0 为主题； 1为标签 2为分类
    int categoryId;
    CategoryModel themeModel;
    TagDetailModel tagDetailModel;
    CategoryListResponseModel goodListModel;
    GridView gridView;
    ImageView imageView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_category);
        source = getIntent().getIntExtra("source",0);
        categoryId = getIntent().getIntExtra("categoryId",0);
        gridView = (GridView)findViewById(R.id.category_grid);
        imageView = (ImageView)findViewById(R.id.image_category);
        getData();


    }
    private void getData(){
        JSONObject data = new JSONObject();
        try {
            if (source==0){

                data.put("userId","");
                data.put("themeId",categoryId);
            }else if (source==1){

                data.put("userId","");
                data.put("tagId",categoryId);
            }else {
                data.put("userId","");
                data.put("categoryId",categoryId);
            }
        } catch (Exception e) {

        }
        String url;
        if (source==0){
            url = "appserver/getThemeDetail";

        }else  if (source==1){
            url = "appserver/getTagDetail";
        }else {
            url = "appserver/getGoodsList";
        }
        RequestAction.sendRequest(this, url, data, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.e("success",jsonObject.toString());

                renderViewByResponse(jsonObject);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e("faile",volleyError.toString());

            }
        });
    }
    private void renderViewByResponse(JSONObject jsonObject){
        try {
            CategoryGridAdapter adapter = null;
            if (source == 0){
                themeModel = JSONHelper.parseObject(jsonObject, CategoryResponseModel.class).getData();
                adapter = new CategoryGridAdapter(this,themeModel.getThemeGoodsList());
                if (themeModel.getThemeRecomList().size()>0){
                    ImageLoader.getInstance().displayImage(themeModel.getThemeRecomList().get(0).getPic(), imageView);

                }
            }else if (source==1){
                tagDetailModel = JSONHelper.parseObject(jsonObject, TagResponseModel.class).getData();
                adapter = new CategoryGridAdapter(this,tagDetailModel.getTagGoodsList());
                ImageLoader.getInstance().displayImage(tagDetailModel.getTagRecomList().get(0).getPic(), imageView);
            }else {
                imageView.setVisibility(View.GONE);
                goodListModel = JSONHelper.parseObject(jsonObject, CategoryListResponseModel.class);
                adapter = new CategoryGridAdapter(this,goodListModel.getData().getList());

            }
            gridView.setAdapter(adapter);
            final List itemModels ;
            if (source == 0){
                itemModels = themeModel.getThemeGoodsList();

            }else  if (source==1){
                itemModels = tagDetailModel.getTagGoodsList();
            }else {
                itemModels = goodListModel.getData().getList();
            }

            gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                    int itemId;
                    if (source==0 || source==1){
                        itemId = ((CategoryItemModel)itemModels.get(position)).getId();
                    }else {
                        itemId = ((GoodModel)itemModels.get(position)).getId();
                    }

                    Intent intent = new Intent(CategoryActivity.this,GoodDetailActivity.class);
                    intent.putExtra("itemId",itemId);
                    startActivity(intent);
                }
            });




        }catch (Exception e){

        }

    }
}
